//
//  SSSMerchantBenefitSearchViewController.m
//  SSSAllRollAgency
//
//  Created by xnxin on 2017/3/16.
//  Copyright © 2017年 Darker. All rights reserved.
//

#import "SSSMerchantBenefitSearchViewController.h"
#import "XHUIHelper.h"
#import "UIStoryboard+XHAdd.h"
#import "SSSMerchantHistoryBenefitCellModel.h"
#import "MHDatePicker.h"
#import "NSDateAddition.h"
#import <QMUIKit/QMUIKit.h>
#import "XHKitMacro.h"
#import "UIColor+Hexadecimal.h"
#import "UIButtonAdditions.h"
#import "SSSMerchantSearchViewCellModel.h"
#import "SSSMerchantBenefitSearchDetailController.h"

@interface SSSMerchantBenefitSearchViewController()

@property (nonatomic, strong) MHDatePicker *datePicker;
@property (nonatomic, assign) BOOL isSelectStartDate;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) QMUIButton *chooseDateButton;
@property (nonatomic, strong) QMUITextField *searchKeyTextField;
@property (nonatomic, strong) SSSMerchantBenefitSearchDetailController *childVC;
@property (nonatomic, strong) NSDate *chooseDate;
@end

@implementation SSSMerchantBenefitSearchViewController



#pragma mark -
#pragma mark LifeCycle


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"收益中心";
    [self.view addSubview:[self headerView]];

    [self addChildViewController:self.childVC];
    [self.view addSubview:self.childVC.view];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.childVC.view.frame = CGRectMake(0, 70, SCREEN_WIDTH, self.view.height - 70);
    self.childVC.view.hidden = true;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark Setter&&Getter

- (SSSMerchantBenefitSearchDetailController *)childVC {

    if (!_childVC) {
        _childVC = [SSSMerchantBenefitSearchDetailController new];
    }

    return _childVC;
}

- (UIView *)headerView {

    if (!_headerView) {

        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 70)];

        UIView *containerView = [UIView new];
        containerView.layer.borderWidth = 1;
        containerView.layer.borderColor = [UIColor colorWithHex:@"55A987"].CGColor;
        containerView.left = 15;
        containerView.top = 15;
        containerView.layer.cornerRadius = 5;
        containerView.width = DEVICE_WIDTH - 30;
        containerView.height = 40;
        containerView.clipsToBounds = true;
        [_headerView addSubview:containerView];

        [containerView addSubview:self.searchButton];
        self.searchButton.right = containerView.width;

        UIButton *btn = [UIButton new];
        btn.tintColor = [UIColor colorWithHex:@"55A987"];
        btn.normalImage = [[UIImage imageNamed:@"icon_date"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        btn.normalTitle = @"请选择日期";
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        btn.normalTitleColor = self.searchButton.backgroundColor;
        [btn addTarget:self action:@selector(tappedStartDateButton:) forControlEvents:UIControlEventTouchUpInside];
        [btn sizeToFit];
        btn.centerY = containerView.height/2;
        btn.centerX = (containerView.width - self.searchButton.width)/4;
        [containerView addSubview:btn];

        _searchKeyTextField = [QMUITextField new];
        _searchKeyTextField.height = 40;
        _searchKeyTextField.userInteractionEnabled = NO;
        _searchKeyTextField.width = (containerView.width - self.searchButton.width)/2;
        _searchKeyTextField.font = [UIFont systemFontOfSize:15];
        _searchKeyTextField.right = self.searchButton.left;
        [containerView addSubview:_searchKeyTextField];

        UIView *hLine = [UIView new];
        hLine.backgroundColor = [UIColor colorWithHex:@"E6E6E6"];
        hLine.height = containerView.height;
        hLine.width = 1;
        [containerView addSubview:hLine];
        hLine.top = 0;
        hLine.centerX = (containerView.width - self.searchButton.width)/2;
    }
    return _headerView;
}

- (UIButton *)searchButton {

    if (!_searchButton) {

        _searchButton = [UIButton new];
        _searchButton.backgroundColor = [UIColor colorWithHex:@"55A987"];
        _searchButton.normalTitle = @" 查询";
        _searchButton.normalImage = [UIImage imageNamed:@"icon_search_history"];
        [_searchButton addTarget:self
                          action:@selector(tappedSearchButton:)
                forControlEvents:UIControlEventTouchUpInside];
        _searchButton.width = 70;
        _searchButton.height = 40;
    }

    return _searchButton;
}

#pragma mark -
#pragma mark PrivateMethod

#pragma mark -
#pragma mark Override

#pragma mark -
#pragma mark Action
- (IBAction)tappedStartDateButton:(UIButton *)sender {

    self.isSelectStartDate = true;

    @weakify(self);
    _datePicker = [MHDatePicker new];
    _datePicker.datePickerMode = UIDatePickerModeDate;
    _datePicker.maxSelectDate = [NSDate date];
    _datePicker.isBeforeTime = YES;

    [self.datePicker didFinishSelectedDate:^(NSDate *selectDataTime) {
        @strongify(self);
        self.chooseDate = selectDataTime;
        self.searchKeyTextField.text = [selectDataTime xhstringWithFormat:@"yyyy-MM-dd"];
    }];
}

- (void)tappedSearchButton:(UIButton *)sender {
    [_searchKeyTextField resignFirstResponder];

    if (self.chooseDate == nil) {
        [XHUIHelper showAutoHideHUDforView:self.view title:@"请选择查询日期"];
        return;
    }
    self.childVC.view.hidden = false;
    self.childVC.chooseDate = self.chooseDate;
    [self.childVC.tableView.mj_header beginRefreshing];
}



@end
